<form action="{:U('setting/edit_process')}" method="post">
	<table class="table table-hover table-striped table_thead_fixed sort-list" width="100%" border="0" cellspacing="1" cellpadding="0">
		<thead>
			<th>审批人</th>
			<th>审批岗位</th>
			<th>操作</th>
		</thead>
	  	<tbody class="process" id="process">
			<volist name="examine_list" id="vo">
				<tr id="step_{$vo['id']}">
					<input type="hidden" class="check_list" name="id[]" value="{$vo['id']}"/>
					<td class="user_name" id="user_name_{$vo['id']}">{$vo['user_info']['user_name']}</td>
					<td class="position_name" id="position_name_{$vo['id']}">{$vo['user_info']['department_name']}-{$vo['user_info']['role_name']}</td>
					<td>
						<a href="javascript:void(0);" class="edit_step" rel="{$vo['id']}">编辑</a>
						<a href="javascript:void(0);" class="delete_step" rel="{$vo['id']}">删除</a>
					</td> 
				</tr>
			</volist>
		</tbody>
	</table>
	<div style="margin-left:5px;margin-top:15px;">
		<a class="add_step" href="javascript:void(0)">+ 添加审批人</a>
	</div>
	<div style="color:#ccc;margin-top:10px;">注：通过拖拽可以修改审批人流程顺序</div>
</form>
<div style="display:none;" id="dialog-edit-step" title="编辑审批人">
	<div class="spiner-example">
		<div class="sk-spinner sk-spinner-three-bounce">
			<div class="sk-bounce1"></div>
			<div class="sk-bounce2"></div>
			<div class="sk-bounce3"></div>
		</div>
	</div>
</div>
<div style="display:none;" id="dialog-add-step" title="增加审批人">
	<div class="spiner-example">
		<div class="sk-spinner sk-spinner-three-bounce">
			<div class="sk-bounce1"></div>
			<div class="sk-bounce2"></div>
			<div class="sk-bounce3"></div>
		</div>
	</div>
</div>
<script type="text/javascript">
$(function(){
	$("table tbody").sortable({
		connectWith: "table tbody",
		stop:function() {
			position = [];
			$.each($(".check_list"), function(i, item){position.push(item.value)});
			$.get('{:U("contract/examinesort")}',{postion:position.join(',')}, function(data){
				if (data.status !== 1) {
					swal({
						title: "顺序保存失败!",
						type: "error"
					});
				}
			}, 'json');
		}
	});

	$("#dialog-edit-step").dialog({
		autoOpen: false,
		modal: true,
		width: 550,
		height: 350,
		resizable: false,
		position:["center",100],
		buttons: {
			"保存": function () {
				var department_id = $("#step_department_id").val();
				if(department_id == ''){
					alert_crm('请选择部门');
					$("#department_id").focus();
					return false;
				}
				var position_id = $("#step_position_id").val();
				if(position_id == ''){
					alert_crm('请选择岗位');
					$("#position_id").focus();
					return false;
				}
				var role_id = $("#step_role_id").val();
				if(role_id == ''){
					alert_crm('请选择审批人');
					$("#role_id").focus();
					return false;
				}
				var step_id = $("#step_id").val();
				$.post("{:U('contract/step')}",
				{role_id:role_id,id:step_id},
				function(data){
					if(data.status == 1){	//修改成功
						$('#position_name_'+step_id).html(data.data.position_name);
						$('#user_name_'+step_id).html(data.data.user_name);
						swal({
							title: data.info,
							type: "success"
						});
					}else{
						swal({
							title: data.info,
							type: "error"
						});
					}
				});
				$(this).dialog("close");
			},
			"取消": function () {
				$(this).dialog("close");
			}
		}
	});

	$(document).on('click','.edit_step',function(){
		var step_id = $(this).attr('rel');
		$('#dialog-edit-step').dialog('open');
		$('#dialog-edit-step').load("{:U('contract/step','id=')}"+step_id);
	});

	$("#dialog-add-step").dialog({
		autoOpen: false,
		modal: true,
		width: 580,
		height: 350,
		resizable: false,
		position:["center",100],
		buttons: {
			"保存": function () {
				var department_id = $("#step_department_id").val();
				if(department_id == ''){
					alert_crm('请选择部门');
					$("#department_id").focus();
					return false;
				}
				var position_id = $("#step_position_id").val();
				if(position_id == ''){
					alert_crm('请选择岗位');
					$("#position_id").focus();
					return false;
				}
				var role_id = $("#step_role_id").val();
				if(role_id == ''){
					alert_crm('请选择审批人');
					$("#role_id").focus();
					return false;
				}

				$.post("{:U('contract/step')}",
				{role_id:role_id},
				function(data){
					if(data.status == 1){
						var temp = '';
						var temp = '<tr id="step_'+data.data.id+'">\
										<input type="hidden" class="check_list" name="id[]" value="'+data.data.id+'"/>\
										<td class="user_name" id="user_name_'+data.data.id+'">'+data.data.user_name+'</td>\
										<td class="position_name" id="position_name_'+data.data.id+'">'+data.data.position_name+'</td>\
										<td><a href="javascript:void(0);" class="edit_step" rel="'+data.data.id+'">编辑</a>&nbsp;<a href="javascript:void(0);" class="delete_step" rel="'+data.data.id+'">删除</a>\
										</td>\
									</tr>';
						$('#process').append(temp);
						// $(".edit_step").click(function(){
						// 	var step_id = $(this).attr('rel');
						// 	$('#dialog-edit-step').dialog('open');
						// 	$('#dialog-edit-step').load("{:U('contract/step','id=')}"+step_id);
						// });
					}else{
						swal({
							title: data.info,
							type: "error"
						});
					}
				});
				$(this).dialog("close");
			},
			"取消": function () {
				$(this).dialog("close");
			}
		}
	});

	$('.add_step').click(function(){
		$('#dialog-add-step').dialog('open');
		$('#dialog-add-step').load("{:U('contract/step')}");
	});

	$(document).on('click','.delete_step',function(){
		var step_id = $(this).attr('rel');
		swal({
			title: "温馨提示",
			text: "您确定要删除吗？",
			type: "warning",   
			showCancelButton: true,   
			confirmButtonColor: "#DD6B55",   
			confirmButtonText: "是的，我要删除！",
	        cancelButtonText:'让我再考虑一下…',
	        closeOnConfirm:false,
	        closeOnCancel:false
		},
		function(isConfirm){
	    	if (isConfirm) {
				$.ajax({
					type:'get',
					url:'index.php?m=contract&a=step_delete&step_id='+step_id,
					async:false,
					success:function(data){
						if(data.status){
							$('#step_'+step_id).remove();
							swal({
								title: data.info,
								type: "success"
							});
						}else{
							swal({
								title: data.info,
								type: "error"
							});
						}
					},
					dataType:'json'
				});
			} else {
	            swal("已取消","您取消了删除操作！","error");
	        }
		});
	});
});
</script>





